package com.tianbo.service.imp;

import com.tianbo.common.ModelAndPage;
import com.tianbo.mapper.UsersMapper;
import com.tianbo.model.Users;
import com.tianbo.model.UsersExample;
import com.tianbo.service.UserService;
import com.tianbo.util.dao.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mrz on 2017/8/23.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UsersMapper userMapper;

    public List<Users> findUser() throws Exception{
        //调用mapper类中的selectByExample方法，如果传入类型为null，则表示无条件查找
        UsersExample example = new UsersExample();
        example.setOrderByClause("username"); //设置order 字段

//        example.or().andAddressLike("datong").andUsernameEqualTo("mrz");//搜索条件

        List<Users> users = userMapper.selectByExample(example);

        return users;
    }

    public void addUser(Users user){
        userMapper.insert(user);
    }

    public  Users selectByUserId(BigDecimal id){
        Users user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    public ModelAndPage userList(Integer dataStart,Integer perPage,Users user)throws Exception{

        //调用mapper类中的selectByExample方法，如果传入类型为null，则表示无条件查找
        UsersExample example = new UsersExample();
        UsersExample.Criteria criteria = example.createCriteria();

        if (user.getMobilephone()!=null){
            criteria.andMobilephoneEqualTo(user.getMobilephone());
        }
        if (user.getUsername()!=null){
            criteria.andUsernameEqualTo(user.getUsername());
        }
        example.setOrderByClause("user_id"); //设置order 字段
        int count = userMapper.countByExample(example); //没加入分页前的搜索总数据数


        Page page = new Page(dataStart,perPage);
        page.setCount(count);

        example.setPage(page); //oracle 用这个

        List<Users> users = userMapper.selectByExample(example);

        ModelAndPage<Users> modelAndPage = new ModelAndPage(users,page);

        return modelAndPage;
    }

    public  int countByExample(UsersExample example){
        return  userMapper.countByExample(example);
    }

    public int updateByPrimaryKey(Users record){
        return userMapper.updateByPrimaryKey(record);
    }

    public int insert(Users record){
        return  userMapper.insert(record);
    }

    public int deleteByPrimaryKey(BigDecimal userId){
        return userMapper.deleteByPrimaryKey(userId);
    }

    /**
     *
     * @param user
     * @return true代表有，FALSE代表无
     */
    public boolean checkUser(Users user){
        //调用mapper类中的selectByExample方法，如果传入类型为null，则表示无条件查找
        UsersExample example = new UsersExample();
        UsersExample.Criteria criteria = example.createCriteria();

        if (user.getUsername()!=null){
            criteria.andUsernameEqualTo(user.getUsername());
        }
        int count = userMapper.countByExample(example);

        if (count>0){
            return true;
        }else {
            return false;
        }

    }
}
